package com.jblog.article.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jblog.article.dao.ArticleDao;
import com.jblog.article.exception.CategoryCannotBeNullException;
import com.jblog.article.model.Article;

@Service
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	ArticleDao articleDao;
	
	/**
	 * 发布文章
	 * 
	 * @param article
	 * @throws CategoryCannotBeNullException 
	 */
	@Transactional(rollbackFor=Exception.class)
	public void add(Article article) throws CategoryCannotBeNullException{
		if(article.getCategory() == null){
			throw new CategoryCannotBeNullException();
		}
		articleDao.add(article);
	}
	
	/**
	 * 修改文章
	 * 
	 * @param article
	 */
	@Transactional(rollbackFor=Exception.class)
	public void update(Article article){
		articleDao.update(article);
	}

	/**
	 * 删除文章
	 * 
	 * @param id
	 */
	@Transactional(rollbackFor=Exception.class)
	public void del(String[] ids){
		for(String id : ids){
			articleDao.delete(new Article(id));
		}
	}

	@Override
	@Transactional
	public Article find(String id) {
		return articleDao.find(id);
	}

}
